package com.changgou.brand.controller;


import changgou.brand.pojo.Brand;
import com.changgou.brand.service.BrandService;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.util.IdWorker;
import com.changgou.util.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 品牌
 */
@RestController
@RequestMapping("/brand")
public class BrandController {


    @Autowired
    private BrandService brandService;

    /**
     * 品牌分页查询
     *
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public Result list(@RequestParam("currentPage") Integer currentPage, @RequestParam("pageSize") Integer pageSize) {

        //品牌查询
        Map<String, Object> map = brandService.findBrandWithPage(currentPage, pageSize, null);
        return new Result(true, StatusCode.OK, "查询成功", map);
    }

    /**
     * 品牌分页条件查询
     *
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/list/{name}")
    public Result list(@PathVariable("name") String name, @RequestParam("currentPage") Integer currentPage, @RequestParam("pageSize") Integer pageSize) {

        //品牌查询
        Map<String, Object> map = brandService.findBrandWithPage(currentPage, pageSize, name);
        return new Result(true, StatusCode.OK, "查询成功", map);
    }


    /**
     * 图片上传
     *
     * @param pic
     * @return
     */
    @PostMapping("/uploadPic")
    public Result uploadPic(@RequestParam("pic") MultipartFile pic) {
        try {
            //获取实际上传文件名
            String originalFilename = pic.getOriginalFilename();
            //获取文件后缀的索引
            int index = originalFilename.lastIndexOf(".");
            //获取文件后缀
            String hz = originalFilename.substring(index);
            //通过UUID获取随机文件名
            String s = UUID.randomUUID().toString();
            //拼接完整文件名
            String fileName = s + hz;
            //调用七牛云工具类，实现文件上传
            QiniuUtils.upload2Qiniu(pic.getBytes(), fileName);

            //添加七牛云域名
            String image = "http://q7yhi9cvn.bkt.clouddn.com/" + fileName;
            System.out.println("图片地址" + image);
            //返回结果
            return new Result(true, StatusCode.OK, "图片上传成功", image);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "图片上传失败");
        }
    }

    /**
     * 品牌新增
     *
     * @param
     * @param brand
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Brand brand) {


        //调用service存储到sql中
        Boolean flag = brandService.add(brand);
        if (flag) {
            return new Result(true, StatusCode.OK, "品牌新增成功");
        }
        return new Result(false, StatusCode.ERROR, "品牌新增失败");

    }

    /**
     * 根据id查询品牌
     *
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(@RequestParam String id) {

        Brand brand = brandService.findById(id);
        return new Result(true, StatusCode.OK, "查询品牌成功", brand);
    }

    /**
     * 更新品牌
     *
     * @param brand
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody Brand brand) {
        Boolean flag = brandService.update(brand);
        if (flag) {
            return new Result(true, StatusCode.OK, "品牌更新成功");
        }
        return new Result(false, StatusCode.ERROR, "品牌更新失败");

    }


    /**
     * 品牌删除
     *
     * @param brandIds
     * @return
     */
    @DeleteMapping("/delete")
    public Result delete( @RequestParam("brandIds") String[] brandIds) {
        brandService.delete(brandIds);
        return new Result(true, StatusCode.OK, "品牌删除成功");
    }
}


